# Contributor: Simon Frankenberger <simon-alpine@fraho.eu>
# Maintainer: Simon Frankenberger <simon-alpine@fraho.eu>
pkgname=openjdk9
pkgver=9.0.4_p12
_pkgver=${pkgver/_p/+}
pkgrel=4
pkgdesc="Oracle OpenJDK 9"
provider_priority=9
url="https://hg.openjdk.java.net/jdk-updates/jdk9u"
# oracle dropped support for 32 bit
# riscv64 blocked by openjdk8
arch="all !x86 !armhf !armv7 !riscv64"
license="GPL-2.0-with-classpath-exception"
makedepends="autoconf
	bash
	gawk
	grep
	make
	openjdk8
	zip
	alsa-lib-dev
	cups-dev
	freetype-dev
	giflib-dev
	lcms2-dev
	elfutils-dev
	libexecinfo-dev
	libffi-dev
	libjpeg-turbo-dev
	libx11-dev
	libxext-dev
	libxrender-dev
	libxt-dev
	libxtst-dev
	linux-headers
	zlib-dev"
depends="$pkgname-jmods $pkgname-demos $pkgname-doc $pkgname-jdk" # for the virtual openjdk9 package
subpackages="$pkgname-jmods:_jmods:noarch
	$pkgname-demos:_demos:noarch
	$pkgname-doc:_doc:noarch
	$pkgname-jre:_jre
	$pkgname-src:_src
	$pkgname-jre-headless:_jre_headless
	$pkgname-jdk:_jdk"
source="jdk-$_pkgver-root.tar.bz2::https://hg.openjdk.java.net/jdk-updates/jdk9u/archive/jdk-$_pkgver.tar.bz2
	jdk-$_pkgver-corba.tar.bz2::https://hg.openjdk.java.net/jdk-updates/jdk9u/corba/archive/jdk-$_pkgver.tar.bz2
	jdk-$_pkgver-hotspot.tar.bz2::https://hg.openjdk.java.net/jdk-updates/jdk9u/hotspot/archive/jdk-$_pkgver.tar.bz2
	jdk-$_pkgver-jaxp.tar.bz2::https://hg.openjdk.java.net/jdk-updates/jdk9u/jaxp/archive/jdk-$_pkgver.tar.bz2
	jdk-$_pkgver-jaxws.tar.bz2::https://hg.openjdk.java.net/jdk-updates/jdk9u/jaxws/archive/jdk-$_pkgver.tar.bz2
	jdk-$_pkgver-jdk.tar.bz2::https://hg.openjdk.java.net/jdk-updates/jdk9u/jdk/archive/jdk-$_pkgver.tar.bz2
	jdk-$_pkgver-langtools.tar.bz2::https://hg.openjdk.java.net/jdk-updates/jdk9u/langtools/archive/jdk-$_pkgver.tar.bz2
	jdk-$_pkgver-nashorn.tar.bz2::https://hg.openjdk.java.net/jdk-updates/jdk9u/nashorn/archive/jdk-$_pkgver.tar.bz2

	build.patch
	aarch64.patch
	ppc64le.patch
	make-4.3.patch
	fix-bootjdk-check.patch
	gcc10-compilation-fix.patch
	JDK-8187578.patch
	JDK-8245051.patch
	JDK-8241296.patch

	HelloWorld.java
	TestECDSA.java
	TestCryptoLevel.java
	Alpine_Bug_10126.java
	gcc11.patch
"
builddir="$srcdir/jdk9u-jdk-$_pkgver"

_java_home="/usr/lib/jvm/java-9-openjdk"

ldpath="$_java_home/lib:$_java_home/lib/jli:$_java_home/lib/server"
sonameprefix="$pkgname:"

# enable running the JTReg tests in check?
# see comment in that function for explanation
_run_jtreg=${_run_jtreg:-0}
if [ $_run_jtreg -ne 0 ]; then
	makedepends="$makedepends java-jtreg"
	checkdepends="$checkdepends ttf-freefont xvfb-run"
fi


unpack() {
	if [ -z "$force" ]; then
		verify
		initdcheck
	fi
	mkdir -p "$srcdir"
	msg "Unpacking root.tar.bz2"
	tar -C "$srcdir" -jxf jdk-$_pkgver-root.tar.bz2

	for i in corba hotspot jaxp jaxws jdk langtools nashorn; do
		msg "Unpacking $i.tar.bz2"
		mkdir $builddir/$i
		tar -C $builddir/$i --strip-components=1 -jxf "$srcdir/jdk-$_pkgver-$i.tar.bz2"
	done
}

prepare() {
	default_prepare

	# update autoconf files to detect alpine
	update_config_sub
	update_config_guess

	# remove not compilable module (hotspot jdk.hotspot.agent)
	# this needs libthread_db which is only provided by glibc
	#
	# haven't found any way to disable this module so just remove it.
	rm -r hotspot/src/jdk.hotspot.agent
}

build() {
	if [ $_run_jtreg -ne 0 ]; then
		_with_jtreg="--with-jtreg=/usr/share/java/jtreg"
	else
		_with_jtreg="--with-jtreg=no"
	fi

	# CFLAGS, CXXFLAGS and LDFLAGS are ignored as shown by a warning
	# in the output of ./configure unless used like such:
	#  --with-extra-cflags="$CFLAGS"
	#  --with-extra-cxxflags="$CXXFLAGS"
	#  --with-extra-ldflags="$LDFLAGS"
	# See also paragraph "Configure Control Variables" from "common/doc/building.md"
	# shellcheck disable=2097 disable=2098
	CFLAGS='' CXXFLAGS='' LDFLAGS='' \
		bash ./configure \
		--openjdk-target=$CHOST \
		--prefix="$_java_home" \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--with-extra-cflags="$CFLAGS" \
		--with-extra-cxxflags="$CXXFLAGS" \
		--with-extra-ldflags="$LDFLAGS" \
		--with-zlib=system \
		--with-libjpeg=system \
		--with-giflib=system \
		--with-libpng=system \
		--with-lcms=system \
		--with-jobs=${JOBS:-4} \
		--with-test-jobs=${JOBS:-4} \
		--with-native-debug-symbols=none \
		$_with_jtreg \
		--disable-warnings-as-errors \
		--disable-precompiled-headers \
		--enable-dtrace=no \
		--with-jvm-variants=server \
		--with-debug-level=release \
		--with-version-pre= \
		--with-version-opt="alpine-r$pkgrel" \
		--with-version-build=${_pkgver#*+}

	# set these flags to create debug builds on build errors:
#		--with-native-debug-symbols=internal \
#		--with-debug-level=fastdebug \

	MAKEFLAGS='' make jdk-image
}

check() {
	local _java_bin="./build/*-normal-server-release/images/jdk/bin"

	# 1) compile and run a simple hello world
	$_java_bin/javac -d . "$srcdir"/HelloWorld.java
	$_java_bin/java HelloWorld

	# 2) compile and run a testcase for unlimited policy
	$_java_bin/javac -d . "$srcdir"/TestCryptoLevel.java
	$_java_bin/java -cp . --add-opens java.base/javax.crypto=ALL-UNNAMED TestCryptoLevel

	# 3) compile and run a testcase for ECDSA signatures
	$_java_bin/javac -d . "$srcdir"/TestECDSA.java
	$_java_bin/java TestECDSA

	# 4) compile and run testcase for bug 10126
	$_java_bin/javac -d . "$srcdir"/Alpine_Bug_10126.java
	$_java_bin/java Alpine_Bug_10126

	# run the gtest unittest suites
	# they don't take long, DO NOT DISABLE THEM!
	MAKEFLAGS='' make test-hotspot-gtest

	# The jtreg tests take very, very long to finish and show some failures (9 - 12 on my machine, varying between runs)
	# I think these are not critical and can be safely ignored.
	# As the tests take too long, they are disabled by default.
	# When updating this aport please let them run at least once on your machine to see if the failure count changes.
	if [ $_run_jtreg -ne 0 ]; then
		_logfile=$( mktemp -p "$builddir" )
		MAKEFLAGS='' xvfb-run make \
			run-test-tier1 \
			run-test-tier2 \
			run-test-tier3 \
			| tee "$_logfile"
		msg "---------------------------------------"
		msg "The build log can be found at $_logfile"
		# abort the build so you may take a look at the logfile
		false
		return 1
	fi
}

package() {
	mkdir -p "$pkgdir/$_java_home"
	cp -r build/*-normal-server-release/images/jdk/* "$pkgdir/$_java_home"
}

_jmods() {
	pkgdesc="Oracle OpenJDK 9 (jmods)"
	depends=""
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/jmods" "$_toroot"
}

_demos() {
	pkgdesc="Oracle OpenJDK 9 (demos)"
	depends=""
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/demo" "$_toroot"
}

_doc() {
	pkgdesc="Oracle OpenJDK 9 (Documentation)"
	depends=""
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/man" "$_toroot"
}

_jre() {
	pkgdesc="Oracle OpenJDK 9 (JRE)"
	depends="$pkgname-jre-headless"
	provides=java-jre
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot/lib"
	mv "$_fromroot/lib/libawt_xawt.so" \
		"$_fromroot/lib/libfontmanager.so" \
		"$_fromroot/lib/libjavajpeg.so" \
		"$_fromroot/lib/libjawt.so" \
		"$_fromroot/lib/libjsoundalsa.so" \
		"$_fromroot/lib/liblcms.so" \
		"$_fromroot/lib/libsplashscreen.so" \
		"$_toroot/lib"
}


_src() {
	pkgdesc="Oracle OpenJDK 9 (sources)"
	depends="$pkgname-jre-headless"
	mkdir -p "$subpkgdir/$_java_home"/lib
	mv "$pkgdir"/$_java_home/lib/src.zip \
		"$subpkgdir"/$_java_home/lib/
}

_jre_headless() {
	pkgdesc="Oracle OpenJDK 9 (JRE headless)"
	depends="java-common java-cacerts"
	provides=java-jre-headless
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/lib" "$_toroot"

	mkdir -p "$_toroot/bin"
	for i in appletviewer \
		idlj \
		java \
		jjs \
		jrunscript \
		keytool \
		orbd \
		pack200 \
		rmid \
		rmiregistry \
		servertool \
		tnameserv \
		unpack200; do
		mv "$_fromroot/bin/$i" "$_toroot/bin/$i"
	done

	mv "$_fromroot/legal"              "$_toroot"
	mv "$_fromroot/conf"               "$_toroot"
	mv "$_fromroot/release"            "$_toroot"
	cp "$builddir/ASSEMBLY_EXCEPTION"  "$_toroot"
	cp "$builddir/LICENSE"             "$_toroot"
	cp "$builddir/README"              "$_toroot"

	# symlink to shared cacerts store
	rm "$_toroot/lib/security/cacerts"
	ln -sf /etc/ssl/certs/java/cacerts \
		"$_toroot/lib/security/cacerts"

	# symlink for java-common to work (expects jre in $_java_home/jre)
	ln -sf . "$_toroot/jre"
}

_jdk() {
	pkgdesc="Oracle OpenJDK 9 (JDK)"
	depends="$pkgname-jre"
	provides=java-jdk
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/bin" "$_toroot"
	mv "$_fromroot/include" "$_toroot"
}

sha512sums="
cef3655ae7db657e6c81aa86587e451e58896bb6ee786495f6d757096334435b6a4de26ec3ec927da2487e135f09ce26414f8d6b9b9c508a28d3087be286b1ec  jdk-9.0.4+12-root.tar.bz2
455998437a9e3ff538b921025d57e19e6fb5148b3f124c9c427c3604689884d81b3ce5c9dbd93d88de26bf1b43ce76d75f75afd95e473a94973a668575e41748  jdk-9.0.4+12-corba.tar.bz2
49d93764b13085a5626bec1c3d4e790f8748c24577a4e990e76bd1006721a5b8b9a256c40bf6419df4dda6f6390e457485f90c1b1101c010476a04d9056e9f16  jdk-9.0.4+12-hotspot.tar.bz2
82f28586fd576cc01062e2ff37a917987775838910e4a5ecdfd096abec1c8d23028b77bfc87a38ada53bc30d71d89bde7408c1ae572a43c87a71ced64fd08d3a  jdk-9.0.4+12-jaxp.tar.bz2
459978a5e3ca21910781daed848229e043eea5bd82dcd20e9249934ec97a2a982126c6e37aac1a36719e3b73a5e6c8a92a50b63033149913614d8f3edfc0846e  jdk-9.0.4+12-jaxws.tar.bz2
259228d3f439dde239e38cdebb8c3bbb83804ab141d87a9c236310707de9c58cd78cd80ceb4c17755cc1048071f24462839988112c2698f7ec1453a8810610f2  jdk-9.0.4+12-jdk.tar.bz2
ef3c70be906a4b0dd9c9195c88da045909ee3ef144941fb7b4495ed66b4162f481095cad87626d2bd38e5a62134b440223cd008dd6123b6b43c00e338610a692  jdk-9.0.4+12-langtools.tar.bz2
848c6ece418e250561572ad704baeb565580098cfc5f849d4e1a3b41b916aae3487eb4d8d0b319f3a503d122ec064ed4de0678d06821c9a2bdb09c990e589c97  jdk-9.0.4+12-nashorn.tar.bz2
c2936ce815595da58337e1d14d484a9b300b8634888dcf27fd0577f176ad4b3655db5487ee0cace7562c82012c7d9c7ab1c1b42a4c5e753ab8efe4f9a2ad6ed3  build.patch
d488b49552ad489e52ea7856fa65ac3bd6301cb14db959729a9e6b590ab09c7dc2aa5a5e7dda6fbd69bde57e7fc24499df67e04f297b4a509e1aed4bf579207c  aarch64.patch
ec0caf9ffcb2a10e9a4ae43d379f4d6a9387398a94a4dd1d09748f1d5c2af0f28f2b573334cba2b44a2fbfaae0d259b41d814f6dc8c3812377f061298c0f1db5  ppc64le.patch
09be87facf942dc2a4711c44939c1e0e557cb931a3db678543c288d3fb1dce85414ad7e6444f8e1c66c26fd170b9cb741dd53d66a1a98bc8200173ab05f583d2  make-4.3.patch
c2cba2987030924afabb4a8fd56cb6173ce3719b67c8374cadedb4b8f4da4efb33ef5df38106101caaf7fdbf9100fce1b1f9ccbf3e4d55cb7266efd508ad8a89  fix-bootjdk-check.patch
846c79eb4ebfdec42a59bdd10996968ac2b1b26b973acf963dba1790ff330f0f33bcaa953c37de312383a1f6b6ea1807f8ac1891c1c9d8b1f21c401e8ec76050  gcc10-compilation-fix.patch
2798a4866e2d34eafc32fe83c768ff5c27179db55312223eb403d43461c701b349818f65112129487a7d628cf679ed560c84f429cd047796277d47b8b4678d1d  JDK-8187578.patch
78925ea347400ee67f21a405f4c5beae6227764f06d85bbefbe3ca977b9bcd9e8a42a514471c836f4e0f65a1ebfc02429c17e10f23d0125c88c76b0d04164362  JDK-8245051.patch
33504e31e99f370a8825c6d321e0b6e7f46eebf0c8adc171a62b63a956d85137fce2992f827651212e8aaf270352d27b26bc4cdbe3ca653e7ddcfc577d24092b  JDK-8241296.patch
d1767dddd8e0956e25c0f77ed45c6fc86a1191bae1704a6dc33be490fd20eaa50461fe5c2a3349512059d555651e2eb41437dd3c1096c351e8ee68b4534a2579  HelloWorld.java
27e91edef89d26c0c5b9a813e2045f8d2b348745a506ae37b34b660fa7093da9a4e0e676ea41dc4a5c901bce02e5304d95e90f68d6c99cbf461b2da40a7a9853  TestECDSA.java
b02dff8d549f88317bb4c741a9e269e8d59eef990197d085388fc49c7423a4eb9367dbe1e02bffb10e7862f5980301eb58d4494e177d0e8f60af6b05c7fbbe60  TestCryptoLevel.java
cc466f64fcc8762cf6e3c1f5739be6425209b27aa58acff4e7eb126003d61fa18266f0e79e57e9d84224654010185ab45dc0a8043543dea227258458a00a1eb1  Alpine_Bug_10126.java
823a85c0c98fef3afb12e2b2081633443cf3158d171ab29beaa6c0ee1be946eced2b62efa2a129b4e66a6d80a7e024807d8e5bdb363ee9d3fcf66fd8c2cdfba5  gcc11.patch
"
